﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoPartsSearch.Domain.Entities;
using AutoPartsSearch.Domain.Entities.Enums;
using System.Security.Principal;
using AutoPartsSearch.Domain.Abstract;
using AutoPartsSearch.Web.ViewModels;

namespace AutoPartsSearch.Web.Controllers
{
    [Authorize(Roles = Role.Seller)]
    public class OfferController : Controller
    {
        private readonly IOfferRepository _offerRepository;
        private readonly ISellerGetter _sellerGetter;
        private readonly IOfferProcessor _offerProcessor;


        public OfferController(IOfferRepository offerRepository, ISellerGetter sellerGetter, IOfferProcessor offerProcessor)
        {
            _offerRepository = offerRepository;
            _sellerGetter = sellerGetter;
            _offerProcessor = offerProcessor;
        }

        public ActionResult Index(IPrincipal user, int page = 1, int pageSize = 20)
        {
            var seller = _sellerGetter.GetSellerByUser(user);
            int totalCount;
            var offers = _offerRepository.GetBySeller(seller.Id, page, pageSize, out totalCount);
            var result = new ViewModels.Offer.List()
                             {
                                 Offers = offers,
                                 PagingInfo = new PagingInfo()
                                 {
                                     CurrentPage = page,
                                     ItemsPerPage = pageSize,
                                     TotalItems = totalCount
                                 }
                             };
            return View(result);
        }

        public ActionResult SendOffer(int id, IPrincipal user)
        {
            var seller = this._sellerGetter.GetSellerByUser(user);
            if (seller.IsBlocked && seller.IsExpired)
            {
                return RedirectToAction("List");
            }
            var offer = new Offer
            {
                QueryId = id
            };
            return View(offer);
        }

        [HttpPost]
        public ActionResult SendOffer(Offer offer, IPrincipal user)
        {
            if (!ModelState.IsValid)
            {
                return View(offer);
            }
            var seller = this._sellerGetter.GetSellerByUser(user);
            if (!(seller.IsBlocked && seller.IsExpired))
            {
                offer.SellerId = seller.Id;
                this._offerRepository.Add(offer);
                this._offerProcessor.Process(offer);
            }
            return RedirectToAction("Confirm");
        }
        
        public ActionResult Confirm()
        {
            return View();
        }

        public PartialViewResult History(IPrincipal user, int queryId)
        {
            var seller = _sellerGetter.GetSellerByUser(user);
            if (seller.IsQuoteEnabled)
            {
                var offers = _offerRepository.GetOffersByQuery(queryId);
                return PartialView(offers.OrderByDescending(x => x.Created));
            }
            return PartialView("NoAccessToHistory");
        }

        public  ActionResult Details(int id, IPrincipal user)
        {
            var seller = this._sellerGetter.GetSellerByUser(user);
            var result = this._offerRepository.GetByIdAndSeller(id, seller.Id);
            return View(result);
        }


    }
}
